using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WaterQualityReportSystem.utils
{
    public class Tools
    {
        public static bool IsValidDouble(string dblValue)
        {

            if (string.IsNullOrEmpty(dblValue))
            {
                return false;
            }

            try
            {
                double.TryParse(dblValue, out double result);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static int ByteToInt32(byte[] bfufers, int startIndex, bool littleEndian)
        {
            if (littleEndian)
            {
                return BitConverter.ToInt32(bfufers, startIndex);
            }
            else
            {
                byte[] reversed = new byte[4];
                Array.Copy(bfufers, startIndex, reversed, 0, 4);
                Array.Reverse(reversed); // 反转数组以转换字节顺序
                return BitConverter.ToInt32(reversed, 0);
            }
        }

        public static uint ByteToUInt32(byte[] bfufers, int startIndex, bool littleEndian)
        {
            if (littleEndian)
            {
                return BitConverter.ToUInt32(bfufers, startIndex);
            }
            else
            {
                byte[] reversed = new byte[4];
                Array.Copy(bfufers, startIndex, reversed, 0, 4);
                Array.Reverse(reversed); // 反转数组以转换字节顺序
                return BitConverter.ToUInt32(reversed, 0);
            }
        }

        public static ushort ByteToUInt16(byte[] bfufers, int startIndex, bool littleEndian)
        {
            if (littleEndian)
            {
                return BitConverter.ToUInt16(bfufers, startIndex);
            }
            else
            {
                byte[] reversed = new byte[2];
                Array.Copy(bfufers, startIndex, reversed, 0, 2);
                Array.Reverse(reversed); // 反转数组以转换字节顺序
                return BitConverter.ToUInt16(reversed, 0);
            }
        }

        public static short ByteToInt16(byte[] bfufers, int startIndex, bool littleEndian)
        {
            if (littleEndian)
            {
                return BitConverter.ToInt16(bfufers, startIndex);
            }
            else
            {
                byte[] reversed = new byte[2];
                Array.Copy(bfufers, startIndex, reversed, 0, 2);
                Array.Reverse(reversed); // 反转数组以转换字节顺序
                return BitConverter.ToInt16(reversed, 0);
            }
        }

        public static float ByteToSingle(byte[] buffers, int startIndex, bool littleEndian)
        {
            byte[] singleBytes = new byte[4];


            Buffer.BlockCopy(buffers, startIndex, singleBytes, 0, 4);

            // 根据需要转换的字节顺序，可能需要对字节数组进行反转
            if (littleEndian)
            {
                Array.Reverse(singleBytes, 0, 4); // 转换为小端字节顺序
            }


            float singleValue = BitConverter.ToSingle(singleBytes, 0);


            return singleValue;
        }

        public static double ByteToDouble(byte[] buffers, int startIndex, bool littleEndian)
        {
            byte[] doubleBytes = new byte[8];


            Buffer.BlockCopy(buffers, startIndex, doubleBytes, 0, 8);

            // 根据需要转换的字节顺序，可能需要对字节数组进行反转
            if (littleEndian)
            {
                Array.Reverse(doubleBytes, 0, 8); // 转换为小端字节顺序
            }


            double doubleValue = BitConverter.ToDouble(doubleBytes, 0);


            return doubleValue;
        }

        public static byte[] hexStringToByteArray(string hexString)
        {

            char[] hexCharacters = hexString.ToCharArray();
            byte[] byteArray = new byte[hexCharacters.Length / 2];

            for (int i = 0; i < byteArray.Length; i++)
            {
                string hexVal = string.Concat(hexCharacters[i * 2], hexCharacters[i * 2 + 1]);
                byteArray[i] = Convert.ToByte(hexVal, 16);
            }

            return byteArray;
        }

        public static string ByteArrayToHexString(byte[] byteArray)
        {
            StringBuilder hexString = new StringBuilder(byteArray.Length * 2);
            foreach (byte b in byteArray)
            {
                hexString.AppendFormat("{0:x2} ", b);
            }
            return hexString.ToString().ToUpper().Trim();
        }

        public static string getSavePath()
        {

            string savePath = ConfigurationManager.AppSettings["SavePath"];
            if (string.IsNullOrEmpty(savePath))
            {
                return new FileInfo(Application.ExecutablePath).Directory.FullName;
            }
            else
            {
                return savePath;
            }
        }

        //Debug.WriteLine(Tools.RoundToEven(5.78851, 3)); //=>5.789
        //Debug.WriteLine(Tools.RoundToEven(5.78850, 3)); //=>5.788
        //Debug.WriteLine(Tools.RoundToEven(5.78860, 3)); //=>5.789
        //Debug.WriteLine(Tools.RoundToEven(5.78840, 3)); //=>5.788
        //Debug.WriteLine(Tools.RoundToEven(5.78750, 3)); //=>5.788
        //Debug.WriteLine(Tools.RoundToEven(5.78751, 3)); //=>5.788
        //Debug.WriteLine(Tools.RoundToEven(5.78760, 3)); //=>5.788
        //Debug.WriteLine(Tools.RoundToEven(5.78740, 3)); //=>5.787
        public static double RoundToEven(double num, int decimalPlaces)
        {
            // 乘以 10 的指定小数位数，将数字转换为整数进行舍入
            num *= Math.Pow(10, decimalPlaces);

            // 调用原始的 RoundToEven 函数进行舍入
            num = RoundToEven(num);

            // 将结果除以 10 的指定小数位数，恢复到原始的小数位数
            num /= Math.Pow(10, decimalPlaces);

            return num;
        }

        public static double RoundToEven(double num)
        {
            // 四舍
            string integerPart = num.ToString().Split('.')[0];

            string decimalPart = null; ;

            if (num.ToString().Split('.').Length == 1)
            {
                decimalPart = "00";
            }
            else if (num.ToString().Split('.')[1].Length == 1)
            {
                decimalPart = num.ToString().Split('.')[1] + "0";
            }
            else
            {
                decimalPart = num.ToString().Split('.')[1].Substring(0, 2);
            }

            string numStr = integerPart + "." + decimalPart;

            String firstNumberAfterPoint = numStr.Substring(numStr.IndexOf(".") + 1, 1);
            String secondNumberAfterPoint = numStr.Substring(numStr.IndexOf(".") + 2, 1);
            String firstNumberBeforePoint = numStr.Substring(numStr.IndexOf(".") - 1, 1);
            if (Int32.Parse(firstNumberAfterPoint) < 5)
            {
                return Math.Floor(num);
            }

            // 六入
            if (Int32.Parse(firstNumberAfterPoint)>= 6)
            {
                return Math.Ceiling(num);
            }

            // 五留双
            if (Int32.Parse(firstNumberAfterPoint) == 5)
            {
                //看后一位
                if (Int32.Parse(secondNumberAfterPoint) > 0)
                {
                    return Math.Ceiling(num);
                }
                else
                {
                    if (Int32.Parse(firstNumberBeforePoint) % 2 == 0)
                    {
                        return Math.Floor(num);
                    }
                    else
                    {
                        return Math.Ceiling(num);
                    }
                }
            }
            else
            {
                throw new Exception("异常的数值");
            }
        }
    }
}
